export default {
  namespaced: true,

  state: ()=>({
    token: localStorage.getItem('token') || '',
    userinfo : JSON.parse(localStorage.getItem('userinfo') || '{}')
  }),

  getters: {
    isLogin(state){
      return state.token.trim().length !==0
    },
    tel(state){
      if(!state.userinfo.phone) return ''
      return state.userinfo.phone.substr(0,3)+ '****' + state.userinfo.phone.substr(7)
    }
  },
  
  mutations: {
    // 设置token
    setToken(state,token){
      if(token){
        state.token = token? token: ''
        localStorage.setItem('token',token)
      }else{
        state.token = ''
        localStorage.removeItem('token')
      }
    },
    // 设置用户信息
    setUserinfo(state,userinfo){
      if(userinfo){
        state.userinfo = userinfo
        localStorage.setItem('userinfo',JSON.stringify(userinfo))
      }else{
        state.userinfo = {}
        localStorage.removeItem('userinfo')
      }
    }
  },
}